package clientService;

import common.Message;
import common.MessageType;
import common.MyFile;

import java.io.*;
import java.util.Date;

/**
 * @author lhn
 * @date 2024-05-16 9:24
 * 用于文件的相关操作
 */

public class FileClientService {
    /**
    * 用于将文件发送给单个用户
    * @author lihaonan
    * @date 2024/5/16 9:26
     * @param src
     * @param dest
     * @param senderId
     * @param getterId
    */
    public void sendFileToOne(String src, String dest, String senderId, String getterId) {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_FILE_MESSAGE);
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setSendTime(new Date().toString());

        //需要将文件进行读取
        FileInputStream fileInputStream = null;
        byte[] fileBytes = new byte[(int) new File(src).length()];
        try {
            fileInputStream = new FileInputStream(src);
            fileInputStream.read(fileBytes);//将src文件读入到字节流
            MyFile myFile = new MyFile(fileBytes,src,dest);
            message.setMyFile(myFile);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //提示信息
        System.out.println("\n 我给" + getterId + "发送文件:" + src +
                "到对方电脑的目录:" + dest);
        try {
            //一个用户只有一个socket
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
        }
    }

    public void sendFileToOneNotDest(String src, String senderId, String getterId) {
        Message message = new Message();
        message.setMesType(MessageType.MESSAGE_FILE_MESSAGE);
        message.setSender(senderId);
        message.setGetter(getterId);
        message.setSendTime(new Date().toString());
        String dest = "";

        //需要将文件进行读取
        FileInputStream fileInputStream = null;
        byte[] fileBytes = new byte[(int) new File(src).length()];
        try {
            fileInputStream = new FileInputStream(src);
            fileInputStream.read(fileBytes);//将src文件读入到字节流
            MyFile myFile = new MyFile(fileBytes,src,dest);
            message.setMyFile(myFile);

        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        //提示信息
        System.out.println("\n 我给" + getterId + "发送文件:" + src);
        try {
            //一个用户只有一个socket
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(ManageClientConnectServerThread.getClientConnectServerThread(senderId).getSocket().getOutputStream());
            objectOutputStream.writeObject(message);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
        }
    }
}
